<?php

namespace app\controller\api;

use app\exception\ErrnoPlus;
use app\service\UserDeviceShareService;
use app\service\UserService;
use app\validate\UserDeviceShareValidate;
use share\controller\ApiBaseController;
use share\exception\ApiException;
use share\service\ContextService;
use share\service\UserBaseService;
use share\service\UserDetailService;
use share\utils\Helper;

class UserDeviceShareController extends ApiBaseController{

    protected $beforeActionList = [
        "checkSign",
        "checkUser", 
    ];

    /**
     * @description: 
     * @return {*}
     * @use: 
     */
    public function initValidate(){        
        $this->validate = new UserDeviceShareValidate();
    }

    /**
     * 用户上传分享
     */
    public function add_by_user_action(){
        $input = $this->check();
        $vendor = ContextService::getVendor();
        $userInfo = ContextService::getUserInfo();
        $res = UserDeviceShareService::addByUser($input,$vendor,$userInfo['id']);
        return self::ok($res);
    }

    /**
     * 管理员上传分享
     */
    public function add_by_admin_action(){
        $input = $this->check();
        if(count($input['info']) < 1){
            throw new ApiException("info detail lost data",ErrnoPlus::INVALID_PARAM);
        }
        foreach($input['info'] as $info){
            $this->check('infoDetail',$info);
        }
        $vendor = ContextService::getVendor();
        $userInfo = ContextService::getUserInfo();
        $res = UserDeviceShareService::addByAdmin($input,$vendor,$userInfo['id']);
        return self::ok($res);
    }

    /**
     * 获取分享规则
     */
    public function get_rule_action(){
        $info = $this->request->param('info/a');
        $userInfo = ContextService::getUserInfo();
        $res = UserDeviceShareService::getShareRule($userInfo['id'],$userInfo['identity_id'],$info);
        if(empty($res)){
            return self::fail("no data",ErrnoPlus::NOT_BACK_DATA);
        }
        return self::ok($res);
    }


    /**
     * 修改分享规则
     */
    public function change_rule_action(){
        $input = $this->check();
        foreach($input['info'] as $info){
            $this->check('changeRuleInfo',$info);
        }
        $userInfo = ContextService::getUserInfo();
        $res = UserDeviceShareService::changeShareRule($input['info'],$userInfo['id']);
        return self::ok($res);
    }

    /**
     * 删除分享规则
     */
    public function del_action(){
        $input = $this->check();
        $info = $this->request->param('info/a');
        if(empty($info)){            
            $userInfo = ContextService::getUserInfo();
            $res = UserDeviceShareService::deleteByUser($userInfo['id']);
        }
        else{
            $res = UserDeviceShareService::deleteAllShareByDevice($info);
        }        
        return self::ok($res);
    }

    /**
     * 删除分享规则V2
     */
    public function del_v2_action(){
        $input = $this->check();
        foreach($input['info'] as $info){
            $this->check('delV2Info',$info);
        }
        $res = UserDeviceShareService::deleteAllShareByDevice($input['info']);
        return self::ok($res);
    }

    /**
     * 获取分享次数
     */
    public function get_count_info_action(){
        $input = $this->check();

        $vendor = ContextService::getVendor();
        $userInfo = ContextService::getUserInfo();
        $res = UserDeviceShareService::getShareCountInfo($input,$userInfo['id'],$vendor);
        return self::ok($res);
    }

    /**
     * 获取被分享者的基本信息
     */
    public function get_share_user_info_action(){
        $input = $this->check();
        $info = $input['info'];
        if(empty($info) || count($info) < 1){
            throw new ApiException("no params to find");
        }
        $users = UserBaseService::getAllByUserId($info);
        if(empty($users)){
            return self::fail("no data",ErrnoPlus::NOT_BACK_DATA);
        }
        $userDetails = UserDetailService::getAll(array_column($users,'id'));
        $res = [];
        foreach($users as $user){
            $detail = Helper::filterOne($userDetails,['id' => $user['id']]);
            $res[] = [
                "shareId" => $user['user_id'],
                "nickName" => $detail ?  $detail['nick_name'] : '',
                "pictureUrl" => $detail ?  $detail['picture'] : '',
            ];
        }
        return self::ok($res);
    }

    /**
     * 用户主动分享
     */
    public function to_share_action(){
        $input = $this->check();
        $info = $input['info'];
        if(empty($info) || count($info) < 1){
            throw new ApiException("no params to find");
        }
        foreach($info as $item){
            $this->check('toShareInfo',$item);
        }
        $vendor = ContextService::getVendor();
        $userInfo = ContextService::getUserInfo();
        $res = UserDeviceShareService::toShare($info,$userInfo['id'],$vendor);
        return self::ok($res);
    }
}